﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Props.Backend;
using System.Configuration;
using Props.AdminWeb.Models;
using System.Web.Security;

namespace Props.AdminWeb.Controllers
{
    public class AccountController : BaseController
    {
        public ActionResult LogIn()
        {
            AccountLogInModel model = new AccountLogInModel()
            {
            };

            return View(model);
        }

        [HttpPost]
        public ActionResult LogIn(string account, string email, string password, bool remember)
        {
            account = account.ToLower();

            var service = new VendorService();

            if (!service.AccountExists(account))
                return Json(new { hasError = true, message = "No existe la cuenta." });
            else
            {
                try
                {
                    if (service.Verify(account, email, password))
                    {
                        string redirectUrl = GetReturnUrl();

                        if (String.IsNullOrWhiteSpace(redirectUrl))
                            redirectUrl = Url.Action("Index", "Home");

                        FormsAuthentication.SetAuthCookie(account, remember);

                        return Json(new { hasError = false, message = "success", redirectUrl = redirectUrl });
                    }
                    else
                    {
                        return Json(new { hasError = true, message = "Verifique los datos ingresados y vuelva a intentarlo." });
                    }
                }
                catch (Exception ex)
                {
                    return Json(new { hasError = true, message = ex.Message });
                }
            }
        }

        public ActionResult LogOut()
        {
            FormsAuthentication.SignOut();

            return RedirectToAction("Index", "Home");
        }

        [HttpPost]
        public ActionResult CheckAccount(string account)
        {
            var service = new VendorService();

            if (service.AccountExists(account))
                return Json(new { hasError = true, message = "El nombre de la cuenta ya está en uso." });
            else
                return Json(new { hasError = false, message = "El nombre de la cuenta está disponible." });
        }

        [HttpPost]
        public ActionResult Register(string account, string email, string password)
        {
            account = account.ToLower();

            var service = new VendorService();

            if (service.AccountExists(account))
                return Json(new { hasError = true, message = "El nombre de la cuenta ya está en uso." });
            else
            {
                try
                {
                    VendorAccount vendorAccount = service.Register(account, email, password);

                    string redirectUrl = Url.Action("Index", "Home");

                    FormsAuthentication.SetAuthCookie(account, false);

                    return Json(new { hasError = false, message = "success", redirectUrl = redirectUrl });
                }
                catch (Exception ex)
                {
                    return Json(new { hasError = true, message = ex.Message });
                }
            }
        }

        private string GetReturnUrl()
        {
            var query = HttpUtility.ParseQueryString(Request.UrlReferrer.Query);
            if (query.AllKeys.Contains("ReturnUrl"))
            {
                return query["ReturnUrl"];
            }

            return null;
        }

    }
}
